package com.xgq.drink.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xgq.drink.entity.Brand;
import com.xgq.drink.entity.FavoriteBrand;
import com.xgq.drink.entity.R;
import com.xgq.drink.service.IBrandService;
import com.xgq.drink.service.IFavoriteBrandService;
import com.xgq.drink.utils.JwtTokenUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xgq.drink.utils.RUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "收藏商家模块", description = "收藏商家")
@RestController
@RequestMapping("/favoriteBrand")
public class FavoriteBrandController {

    @Autowired
    private IFavoriteBrandService favoriteBrandService;
    @Autowired
    private IBrandService brandService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    /**
     * 添加收藏品牌
     */
    @PostMapping("/add")
    public R addFavoriteBrand(@RequestParam Long brandId, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String userId = jwtTokenUtil.getUserIdFromToken(token);
        if (favoriteBrandService.isFavoriteBrand(Long.parseLong(userId), brandId)) {
            return RUtils.fail("已收藏");
        }
        FavoriteBrand favoriteBrand = new FavoriteBrand();
        favoriteBrand.setBrandId(brandId);
        favoriteBrand.setUserId(Long.parseLong(userId));
        favoriteBrand.setCreatedAt(LocalDateTime.now());
        favoriteBrand.setIsDel(0);
        if (favoriteBrandService.save(favoriteBrand)) {
            return RUtils.success("收藏成功");
        }else {
            return RUtils.fail("收藏失败");
        }
    }

    /**
     * 删除收藏品牌
     */
    @DeleteMapping("/delete")
    public R deleteFavoriteBrand(@RequestParam Long favoriteBrandId, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String userId = jwtTokenUtil.getUserIdFromToken(token);
        if (favoriteBrandService.logicDeleteByUserIdAndFavoriteBrandId(Long.parseLong(userId), favoriteBrandId)==1) {
            return RUtils.success("删除成功");
        } else {
            return RUtils.fail("删除失败");
        }
    }
    /**
     * 分页查询收藏品牌
     */
    @GetMapping("/list")
    public IPage<Brand> getFavoriteBrandList(@RequestParam(value = "current", defaultValue = "1") int current,
                                             @RequestParam(value = "size", defaultValue = "10") int size, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String userId = jwtTokenUtil.getUserIdFromToken(token);
        Page<FavoriteBrand> page = new Page<>(current, size);
        // 查询条件：用户ID匹配且isDel为空
        QueryWrapper<FavoriteBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", Long.parseLong(userId));
        queryWrapper.eq("is_del", 0);

        // 先查询收藏品牌表
        IPage<FavoriteBrand> favoriteBrandPage = favoriteBrandService.page(page, queryWrapper);

        // 提取品牌ID
        List<Long> brandIds = favoriteBrandPage.getRecords().stream()
                .map(FavoriteBrand::getBrandId)
                .collect(Collectors.toList());

        // 创建新的分页对象和查询条件
        Page<Brand> brandPage = new Page<>(current, size);
        QueryWrapper<Brand> brandQueryWrapper = new QueryWrapper<>();
        brandQueryWrapper.in("brand_id", brandIds);

        // 查询品牌列表
        return brandService.page(brandPage, brandQueryWrapper);
    }
}